import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import '../pages/login.dart';
import '../pages/not_found.dart';
import '../pages/register.dart';
import '../pages/Home/index.dart';
import '../pages/Details/index.dart';

class Routes {
  static const String home = '/';
  static const String login = '/login';
  static const String register = '/register';
  static const String roomDetail = '/room/:roomId';
  static FluroRouter router = FluroRouter();

  static Handler loginHandler = Handler(
    handlerFunc: (context, Map<String, List<String>> params) {
      return LoginPage();
    },
  );
  static Handler registerHandler = Handler(
    handlerFunc: (context, Map<String, List<String>> params) =>
        const RegisterPage(),
  );

  static Handler homeHandler = Handler(
    handlerFunc: (context, Map<String, List<String>> params) =>
        const HomePage(),
  );

  static Handler notFoundHandler = Handler(
    handlerFunc: (context, Map<String, List<String>> params) =>
        const NotFound(),
  );

  static Handler roomDetailHandler = Handler(
    handlerFunc: (context, Map<String, List<String>> params) {
      final roomId = params['roomId']?.isNotEmpty == true
          ? params['roomId']![0]
          : '';
      return RoomDetailPage(roomId: roomId);
    },
  );

  static void configureRoutes() {
    router.notFoundHandler = Handler(
      handlerFunc: (_, __) {
        // 找不到路由时，返回404页面
        return Scaffold(
          appBar: AppBar(),
          // 这是自行封装的Empty组件
          body: const Center(child: Text("404")),
        );
      },
    );
    // 初始化路由信息
    {
      router.define(home, handler: homeHandler);
      router.define(login, handler: loginHandler);
      router.define(roomDetail, handler: roomDetailHandler);
      router.define(register, handler: registerHandler);
      router.notFoundHandler = notFoundHandler;
    }
  }
}
